﻿using System;
using System.Timers;
using UnityEngine;

namespace Assets.Scripts.Tool
{
    /// <summary>
    /// 功能：计时器工具
    /// 修改时间：2017/6/7
    /// </summary>
    public class TimerTool
    {
        /// <summary>
        /// 得到 小时，分钟，秒
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public static string GetTime(float timer)
        {
            //得到小时
            int hour = Mathf.FloorToInt(timer / 3600);
            //得到分钟
            int minute = Mathf.FloorToInt(timer / 60) - hour * 60;
            //得到秒
            int second = (int)timer - hour * 3600 - minute * 60;
            return hour + "小时" + minute + "分钟" + second + "秒";

        }

        /// <summary>
        /// 得到天数，小时
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public static string GetTime01(float timer)
        {
            //得到天数
            int day = Mathf.FloorToInt(timer / 86400);
            int hour = Mathf.FloorToInt((timer - day * 86400) / 3600);
            return day + "天" + hour + "小时";
        }

        /// <summary>
        /// 得到小时，分钟
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public static string GetTime02(float timer)
        {
            //得到小时
            int hour = Mathf.FloorToInt(timer / 3600);
            //得到分钟
            int minute = Mathf.FloorToInt(timer / 60) - hour * 60;
            return hour + "小时" + minute + "分钟";
        }

        /// <summary>
        /// 得到分钟，秒
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public static string GetTime03(float timer)
        {
            //得到分钟
            int minute = Mathf.FloorToInt(timer/60);
            //得到秒
            int second = (int)timer - minute*60;
            string s_minute = "",s_second="";
            if (minute<10)
            {
                s_minute = 0 + minute.ToString();
            }
            else
            {
                s_minute = minute.ToString();
            }
            if (second < 10)
            {
                s_second = 0 + second.ToString();
            }
            else
            {
                s_second = second.ToString();
            }
            return s_minute+":"+s_second;
        }

        /// <summary>
        /// 比较time1和time2的时间差返回秒,时间差在一天以内
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public static int CompareTime(DateTime time1, DateTime time2)
        {
            if (time1.CompareTo(time2) == 1)//说明time1 在time2 之后
            {
                if (time1.Hour.CompareTo(time2.Hour) == -1)
                {
                    int timer = (time1.Hour + 24 - time2.Hour) * 3600 + (time1.Minute - time2.Minute) * 60 + time1.Second -
                           time2.Second;
                    return timer;
                }
                else
                {
                    int timer = (time1.Hour - time2.Hour) * 3600 + (time1.Minute - time2.Minute) * 60 + time1.Second -
                          time2.Second;
                    return timer;
                }
            }
            else//说明time1 在time2 之前
            {
                if (time2.Hour.CompareTo(time1.Hour) == -1)
                {
                    int timer = (time2.Hour + 24 - time1.Hour) * 3600 + (time2.Minute - time1.Minute) * 60 + time2.Second -
                       time1.Second;
                    return timer;
                }
                else
                {
                    int timer = (time2.Hour - time1.Hour) * 3600 + (time2.Minute - time1.Minute) * 60 + time2.Second -
                         time1.Second;
                    return timer;
                }
            }
        }

        /// <summary>
        /// 比较time1和time2的时间差返回正数秒
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public static int CompareTime01(DateTime time1, DateTime time2)
        {
            TimeSpan timer = time1.Subtract(time2).Duration();
            return timer.Seconds + timer.Minutes * 60 + timer.Hours * 3600 + timer.Days * 24 * 60 * 60;
        }

        /// <summary>
        /// 设置计时器
        /// </summary>
        /// <param name="time">计时的时间秒</param>
        /// <param name="timeOutEvent">计时到达后执行的事件</param>
        public static void SetTimer(float time, Action<object, ElapsedEventArgs> timeOutEvent)
        {
            Timer t = new Timer(time * 1000); //实例化Timer类，设置间隔时间为10000毫秒；   
            t.Elapsed += new ElapsedEventHandler(timeOutEvent); //到达时间的时候执行事件；   
            t.AutoReset = true; //设置是执行一次（false）还是一直执行(true)；   
            t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件；
        }
    }
}